<template>
  <div class="tree-edit-box">
    <rx-interface-category-tree
      ref="categoryTree"
      :interfaceData="interfaceData"
      :expanded_Keys="expandedKeys"
      :selected_Keys="selectedKeys"
      :searchValue="searchValue"
      @select="handSelect"
      @expand="onExpand"
    >
      <template slot="edit" slot-scope="{tree,data}" v-if="tree.key !== 'all'">
        <div class="tree-btn" v-if="tree.parentId == '-1'" @click="grantClassify(tree)">{{i18n('classify.editClassify','编辑分类')}}</div>
        <div class="tree-btn" v-if="tree.parentId == '-1'" @click="deleteClassify(tree)">{{i18n('classify.deleteClassify','删除分类')}}</div>
        <div class="tree-btn" v-if="tree.parentId == '-1'" @click="addInterface(tree)">{{i18n('classify.addInterface','添加接口')}}</div>
        <div class="tree-btn" v-if="tree.parentId != '-1'" @click="deleteInterface(tree)">{{i18n('classify.deleteInterface','删除接口')}}</div>
      </template>
    </rx-interface-category-tree>
  </div>
</template>

<script>
  import sysInterfaceApiAdd from './SysInterfaceApiAdd'
  import sysInterfaceClassificationEdit from './SysInterfaceClassificationEdit'
  import sysInterfaceApiApi from '@/api/system/core/sysInterfaceApi'
  import sysInterfaceClassificationApi from '@/api/system/core/sysInterfaceClassification'
  import {Util,RxInterfaceCategoryTree} from "@lowcode/jpaas-common-lib";

    export default {
      name: "sys-interface-category-treeeditor",
      components: {
        RxInterfaceCategoryTree
      },
      props: {
        projectId:{
          type:String
        },
        interfaceData:{
          type:Array
        },
        expandedKeys:{
          type:Array
        },
        selectedKeys:{
            type:Array
        },
        searchValue:{
          type:String
        }
      },
      computed: {
        categoryTree() {
          return this.$refs.categoryTree;
        },
        curRow() {
          return this.$refs.categoryTree.curRow;
        },
      },
      methods: {
          i18n(name, text, key) {
              return this.$ti18(name, text, "sysInterfaceProject", key);
          },
        handSelect(selKeys, e) {
          if(e && Object.keys(e).length === 0){
              return
          }
          this.$emit("handSelect", this.curRow);
        },
        onExpand(expandedKeys){
            this.$emit("expand",expandedKeys);
        },
        //编辑分类
        grantClassify(record){
          var conf = {
            component: sysInterfaceClassificationEdit,
            curVm: this,
            widthHeight: ['800px','400px'],
            title: this.i18n('classify.editClassify','编辑分类'),
            data:{
              pkId:record.treeId
            }
          }
          var self_ = this;
          Util.open(conf, function(action) {
            if(action != 'ok') {
              return;
            }
            self_.$bus.$emit("apiLoadEvent");
          });
        },
        //删除分类
        deleteClassify(record){
          var treeId = record.treeId;
          let self_ = this;
          this.$confirm({
            title: self_.$ti18('rxMsa.actionTips','操作提示'),
            content: self_.$ti18('rxMsa.delInfo','您确定需要删除选中的记录吗？'),
              okText: self_.$ti18('rxForm.ackBtn','确认'),
              cancelText: self_.$ti18('rxForm.cancelBtn','取消'),
            zIndex:9999,
            onOk() {
              sysInterfaceClassificationApi.del({ ids: treeId }).then(res => {
                if (res.success && res.code == 200) {
                    self_.$bus.$emit("apiLoadEvent");
                    self_.$bus.$emit("apiTableLoadEvent","all");
                }
              })
            },
            onCancel() {
            }
          })
        },
        //添加接口
        addInterface(record){
          var conf = {
            component: sysInterfaceApiAdd,
            curVm: this,
            widthHeight: ['800px','400px'],
            title: this.i18n('classify.addInterface','添加接口'),
            data:{projectId:this.projectId,classificationId:record.treeId}
          }
          var self_ = this;
          Util.open(conf, function(action) {
            if(action != 'ok') {
              return;
            }
            self_.$bus.$emit("apiLoadEvent");
            self_.$bus.$emit("apiFormLoadEvent",this.resultData.apiId,this.resultData.classificationId);
          });
        },
        //删除接口
        deleteInterface(record){
          var id = record.pkId;
          let self_ = this;
          this.$confirm({
            title: self_.$ti18('rxMsa.actionTips','操作提示'),
            content: self_.$ti18('rxMsa.delInfo','您确定需要删除选中的记录吗？'),
              okText: self_.$ti18('rxForm.ackBtn','确认'),
              cancelText: self_.$ti18('rxForm.cancelBtn','取消'),
            zIndex:9999,
            onOk() {
                sysInterfaceApiApi.del({ ids: id }).then(res => {
                if (res.success && res.code == 200) {
                  self_.$bus.$emit("apiLoadEvent");
                  self_.$bus.$emit("apiTableLoadEvent",record.pkId);
                }
              })
            },
            onCancel() {
            }
          })
        },
      }
    }
</script>

<style scoped>
.tree-btn{
  text-align:center;
}
.tree-edit-box  >>> .rx-v-tree-edit:has(.rx-v-tree-fold-content > div:empty) > .rx-v-tree-edit-click-box {
    display: none!important;
}
</style>